package plotting;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import data.Data;
import data.DataUtils;


public class Plot {
	/* fields */
	JPanel panel;
	List<XYSeries> seriesList;
	XYSeriesCollection dataset;
	JFreeChart chart;
	XYPlot plot;
	ChartPanel chartPanel;
	JFrame frame;
	static Logger logger = Logger.getLogger(Plot.class);
	
	public Plot(Data... data){
		seriesList = createAllSeriesForData(data);
		dataset = createAllSeries(seriesList);
		chart = createChart(dataset);
		createPlotAndPanel();
		
	}
	
	private void createPlotAndPanel() {
		logger.info("creating a plot");
		plot = (XYPlot) chart.getXYPlot();
		
		chartPanel = new ChartPanel(chart);
		panel = new JPanel();
		panel.add(chartPanel);
		
	}
	
	public void plot(){
		frame = new JFrame("Plot");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(panel);
		frame.pack();
		frame.setVisible(true);
	}

	private JFreeChart createChart(XYSeriesCollection dataset) {
		JFreeChart chart = ChartFactory.createXYLineChart(
				"title", // chart title
				"x", // x label
				"y", // ylabel
				dataset, // dataset
				PlotOrientation.VERTICAL, // plotorientation
				true, // legend
				true, // tootltips
				true // urls
		);
		return chart;
	}

	static List<XYSeries> createAllSeriesForData(Data[] data){
		
		List<XYSeries> list = new ArrayList<XYSeries>(data.length);
		List<XYSeries> innerList = new ArrayList<XYSeries>();
		for(int i=0;i<data.length;i++){
			innerList = getListForDataObject(data[i]);
			list.addAll(innerList);
		}
		return list;
	}
	
	
	static XYSeriesCollection createAllSeries(List<XYSeries> seriesList) {
		XYSeriesCollection dataset = new XYSeriesCollection();
		for(XYSeries series : seriesList){
			dataset.addSeries(series);
		}
		
		return dataset;
	}


	public static XYSeries createSeries(String dataName, double[] independent,
			double[] dependent) {
		if(independent.length != dependent.length){
			throw new IllegalArgumentException();
		}
		
		XYSeries series = new XYSeries(dataName);
		for(int i=0;i<independent.length;i++){
			series.add(independent[i], dependent[i]);
		}
		
		return series;
	}
	
	public XYSeriesCollection getDataset() {
		return dataset;
	}

	public static List<XYSeries> getListForDataObject(Data input) {
		
		List<XYSeries> list = new ArrayList<XYSeries>();
		int nDeps = input.getDependents().length;
		double[] independent = input.getIndependent();
		for(int i=0;i<nDeps;i++){
			String seriesName = input.getColumnNames()[i+1];
			double[] dependent = input.getDependents()[i];
			XYSeries series = createSeries(seriesName, independent, dependent);
			list.add(series);
		}
		return list;
	}
	
	public static void main(String[] args) {
		BasicConfigurator.configure();
		String dataDir = "test/data_test_dir/";
		Data d1 = Data.readDataFromFile("test1", dataDir+"dataTest.txt");
		
		Data d2 = DataUtils.intersect(d1, 1, d1, 2);
		//TODO: change the way the series are named and the plts are named and the
		//axisLabels are named, etc.
		Plot p = new Plot(d1);
		p.plot();
		Plot p2 = new Plot(d2);
		p2.plot();
	}
	
	//create a JPanel
	//create any series
	//add the points to the series
	//creating the data set
	// add the series to the dataset
	//create the chart, providing labels, and adding the dataset
	// create a plot from the chart
	// set any plot parameters , range markers, stroke, etc
	// create a chart panel around the chart
	// ad the chart panel to the JPanel
	
	//create a JFrame, set the Frame up. add the 
	// add the JPanel to the contentPane
}